package com.shzu.jwc.ty.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shzu.jwc.ty.bean.*;
import com.shzu.jwc.ty.common.ExportExcelUtils;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.param.StartandEndDate;
import com.shzu.jwc.ty.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.common.Semester;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("knowledgetest")
public class TheoreticalExaminationcontroller {
	
	@Autowired
	TheoreticalExaminationservice theoreticalExaminationservice;
	
	@Autowired
	PaperModelservice paperModelservice;
	
	@Autowired
	SystemYearservice systemYearservice;
	
	@Autowired
	UserTeacherInfoservice userTeacherInfoservice;
	
	@Autowired
	InvigilatorInfoservice invigilatorInfoservice;

	@Autowired
	ExamWeekService examWeekService;

	@RequestMapping("/knowledgetest.action")
	public String showknowledetest(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model)
	{
		Page page=new Page(theoreticalExaminationservice.countknowlege(),pn);
		page.setPageSize(20);
		List<TheoreticalExamination> data=theoreticalExaminationservice.getknowlege(page);
		List<TheoreticalPaperModel> papermodel=paperModelservice.getpapermodel();
		model.addAttribute("data", data);
		model.addAttribute("page",page);
		model.addAttribute("papermodel", papermodel);
		model.addAttribute("week",examWeekService.byTypegetWeek("体育知识考试"));
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
	
		return "knowledgetest/knowledgetest";
	}
	
	@RequestMapping("/selectdate.action")
	public String selectdate(@RequestParam("systemYearId") Integer systemYearId,@RequestParam(value="pn",defaultValue="1") Integer pn,Model model,HttpSession session)
	{
		List<TheoreticalPaperModel> papermodel=paperModelservice.getpapermodel();
		if(systemYearId==0)
		{
			Page page=new Page(theoreticalExaminationservice.countknowlege(),pn);
			page.setPageSize(20);
			List<TheoreticalExamination> data=theoreticalExaminationservice.getknowlege(page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		else {
			Page page=new Page(theoreticalExaminationservice.countselectknowlege(systemYearId),pn);
			page.setPageSize(20);
			List<TheoreticalExamination> data=theoreticalExaminationservice.bydatesearch(systemYearId,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
			session.setAttribute("systemYearId", systemYearId);
		}
		model.addAttribute("papermodel", papermodel);
		model.addAttribute("week",examWeekService.byTypegetWeek("体育知识考试"));
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "knowledgetest/knowledgetest";
	}

	@RequestMapping("/selectweek.action")
	public String selectweek(@RequestParam("systemExamWeeklyId") Integer systemExamWeeklyId,@RequestParam(value="pn",defaultValue="1") Integer pn,Model model,HttpSession session){
		List<TheoreticalPaperModel> papermodel=paperModelservice.getpapermodel();
		if(systemExamWeeklyId==0)
		{
			Page page=new Page(theoreticalExaminationservice.countknowlege(),pn);
			page.setPageSize(20);
			List<TheoreticalExamination> data=theoreticalExaminationservice.getknowlege(page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		else {
			Page page=new Page(theoreticalExaminationservice.countselectweek(systemExamWeeklyId),pn);
			page.setPageSize(20);
			List<TheoreticalExamination> data=theoreticalExaminationservice.byweeksearch(systemExamWeeklyId,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
			session.setAttribute("systemExamWeeklyName",examWeekService.byIDgetname(systemExamWeeklyId));
			session.setAttribute("systemExamWeeklyId", systemExamWeeklyId);
		}
		model.addAttribute("papermodel", papermodel);
		model.addAttribute("week",examWeekService.byTypegetWeek("体育知识考试"));
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "knowledgetest/knowledgetest";
	}
	
	@ResponseBody
	@RequestMapping("/insertknowledgetest.action")
	public Msg insert(TheoreticalExamination theoreticalExamination)
	{
		try{
			theoreticalExaminationservice.inserttheoretical(theoreticalExamination);
			return Msg.success("添加成功").add("systemExamWeeklyId",theoreticalExamination.getSystemExamWeeklyId());
		}catch(Exception e){
			e.printStackTrace();
			return Msg.error("添加失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteknowledgetest.action")
	public Msg deleteknowledgetest(@RequestParam("id") Integer theoreticalExaminationId)
	{
		if(theoreticalExaminationservice.delete(theoreticalExaminationId)>0)
			return Msg.success("取消成功");
		else
			return Msg.error("取消失败");
	}

	@ResponseBody
	@RequestMapping("/copyone.action")
	public Msg copyone(@RequestParam("id") Integer theoreticalExaminationId){
		try{
			theoreticalExaminationservice.copyone(theoreticalExaminationId);
			return Msg.success("操作成功").add("systemExamWeeklyId",theoreticalExaminationservice.getone(theoreticalExaminationId).getSystemExamWeeklyId());
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}
	}
	
	@RequestMapping("/update.action")
	public String update(@RequestParam("id") Integer theoreticalExaminationId,Model model)
	{
		List<TheoreticalPaperModel> papermodel=paperModelservice.getpapermodel();
		model.addAttribute("papermodel", papermodel);
		model.addAttribute("week",examWeekService.byTypegetWeek("体育知识考试"));
		model.addAttribute("data",theoreticalExaminationservice.getone(theoreticalExaminationId));
		return "knowledgetest/update";
	}
	
	@ResponseBody
	@RequestMapping("/updatefun.action")
	public Msg updatefun(TheoreticalExamination theoreticalExamination)
	{
		theoreticalExamination.setTheoreticalExaminationState(0);
		if(theoreticalExaminationservice.update(theoreticalExamination)>0)
			return Msg.success("操作成功");
		else
			return Msg.error("操作失败");
	}
	
	@RequestMapping("/searchknowledgetest.action")
	public String searchknowledgetest(@RequestParam(value="pn",defaultValue="1") Integer pn,@RequestParam("theoreticalExaminationName") String theoreticalExaminationName,Model model)
	{
		PageHelper.startPage(pn, 20);
		List<TheoreticalExamination> data=theoreticalExaminationservice.search(theoreticalExaminationName);
		List<TheoreticalPaperModel> papermodel=paperModelservice.getpapermodel();
		model.addAttribute("data", data);
		model.addAttribute("papermodel", papermodel);
		model.addAttribute("week",examWeekService.byTypegetWeek("体育知识考试"));
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "knowledgetest/knowledgetest";
	}
	
	@ResponseBody
	@RequestMapping("/searchteacher.action")
	public Msg searchteacher(@RequestParam("value") String userTeacherInfoName)
	{
		return Msg.success("操作成功").add("teacherdata", userTeacherInfoservice.serach(userTeacherInfoName));
	}
	
	@ResponseBody
	@RequestMapping("/addinvigilatorinfo.action")
	public Msg addinvigilatorinfo(InvigilatorInfo invigilatorInfo)
	{
		invigilatorInfo.setExamType("理论考试");
		Integer statue=invigilatorInfoservice.insert(invigilatorInfo);
		if(statue>0) {
			
			return Msg.success("插入成功").add("invigilatorInfo", invigilatorInfoservice.selectone(statue));
		}
		else if(statue==-1)
			return Msg.error("已安排过该老师");
		else
			return Msg.error("插入失败");
	}
	
	@ResponseBody
	@RequestMapping("/selectjk.action")
	public Msg selectjk(Integer examId)
	{
		return Msg.success("操作成功").add("data", invigilatorInfoservice.selectbyempleid(examId,"理论考试"));
	}
	
	@ResponseBody
	@RequestMapping("/deletejk.action")
	public Msg deletejk(Integer invigilatorInfoId)
	{
		try
		{
			invigilatorInfoservice.delete(invigilatorInfoId);
			return Msg.success("删除成功");
		}catch(Exception ex)
		{
			ex.printStackTrace();
			return Msg.error("删除失败");
		}
		
	}

	@RequestMapping("/showprint.action")
	public String showprint(Model model){
		model.addAttribute("data",theoreticalExaminationservice.showprint());
		return "knowledgetest/printexam";
	}

	@RequestMapping("/selectshowprint.action")
	public String selectshowprint(StartandEndDate startandEndDate, Model model){
		model.addAttribute("data",theoreticalExaminationservice.selectshowpint(startandEndDate));
		return "knowledgetest/printexam";
	}

	@RequestMapping("/export.action")
	public ModelAndView export(StartandEndDate startandEndDate, HttpServletResponse response){
		List<PeTheoreticalTeacher> data=theoreticalExaminationservice.selectshowpint(startandEndDate);
		String title="体育知识考试安排";
		String[] rowsName=new String[]{"考试时间","体育知识考试","考试地点","考试人数","主监考老师","副监考老师"};
		List<Object[]> datalist=new ArrayList<Object[]>();
		Object[] objs=null;
		SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat time=new SimpleDateFormat("HH:mm");
		for (int i=0;i<data.size();i++){
			PeTheoreticalTeacher po=data.get(i);
			objs=new Object[rowsName.length];
			objs[0]=date.format(po.getTheoreticalExaminationStartTime())+" "+time.format(po.getTheoreticalExaminationStartTime())+"-"+time.format(po.getTheoreticalExaminationEndTime());
			objs[1]=po.getTheoreticalExaminationName();
			objs[2]=po.getTheoreticalExaminationPlace();
			objs[3]=po.getTheoreticalExaminationUpperLimit()+"";
			objs[4]=po.getNameExp6();
			objs[5]=po.getNameExp7();
			datalist.add(objs);
		}
		ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, datalist,response);
		ex.exportData();
		return null;
	}

	@ResponseBody
	@RequestMapping("/importStu.action")
	public Msg importStu(@RequestParam("theoreticalExaminationId") Integer theoreticalExaminationId,@RequestParam("file") MultipartFile file){
		if(file.isEmpty())
			return Msg.error("文件不能为空");
		String name=file.getOriginalFilename();
		if(!name.endsWith(".xlsx")) {
			return Msg.error("文件类型必须为xlsx");
		}
		try {
			theoreticalExaminationservice.importStu(name,file,theoreticalExaminationId);
			return Msg.success("导入成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("导入失败");
		}
	}

	@ResponseBody
	@RequestMapping("/importThe.action")
	public Msg importThe(@RequestParam("thefile") MultipartFile file){
		if(file.isEmpty())
			return Msg.error("文件名不能为空");
		String name=file.getOriginalFilename();
		if(!name.endsWith(".xlsx"))
			return Msg.error("文件类型必须为xlsx");
		try{
			theoreticalExaminationservice.importThe(name,file);
			return Msg.success("导入成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("导入失败");
		}
	}
}
